package com.fs.m.server.function.test;

import com.fs.m.server.domain.RequestContext;
import com.fs.m.server.domain.Result;
import com.fs.m.server.domain.ResultCode;
import com.fs.m.server.function.Function;
import com.fs.m.server.util.MapUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 搜索时默认显示的几个关键词
 *
 * User: zhuguoyin
 * Date: 13-10-7
 * Time: 下午8:13
 * To change this template use File | Settings | File Templates.
 */
public class HotwordFunction1 implements Function {

    String[] allKeyword = {"2B", "耍贱", "欢乐", "女汉子", "爱情"};

    @Override
    public Result execute() throws Exception {
        Result result = new Result();
        List list = new ArrayList();
        Map param = RequestContext.get().getParam();
        int count;
        if (param == null || !param.containsKey("count")) {
            count = 5;
        } else {
            try {
                count = MapUtil.getInt(param, "count");
            } catch (Exception e) {
                result.setCode(ResultCode.PARAMETER_ERROR.getCode());
                return result;
            }
        }
        if (count < allKeyword.length) {
            for (int i = 0; i < count; i++) {
                list.add(allKeyword[i]);
            }
        } else {
            for (int i = 0; i < count; i++) {
                list.add(allKeyword[new Random().nextInt(allKeyword.length)]);
            }
        }
        result.addResultData("hotwords", list);
        return result;
    }


}
